#include "dragwidgetfilter.h"

DragWidgetFilter::DragWidgetFilter(QObject *parent) : QObject(parent)
{

}

bool DragWidgetFilter::eventFilter(QObject *watched, QEvent *event)
{
    auto window = dynamic_cast<QWidget*>(watched);
    if (!window)
        return false;
    if (event->type() == QEvent::MouseButtonPress)
    {
        auto mouseEvent = dynamic_cast<QMouseEvent*>(event);
        if (!mouseEvent)
            return false;
        this->pos = mouseEvent->pos();
    }
    else if (event->type() == QEvent::MouseMove)
    {
        auto mouseEvent = dynamic_cast<QMouseEvent*>(event);
        if (!mouseEvent)
            return false;
        if (mouseEvent->buttons() & Qt::MouseButton::LeftButton && this->pos.ry() < 25)
            window->move(mouseEvent->globalPos() - this->pos);
    }
    return QObject::eventFilter(watched, event);
}
